#!/bin/bash
#https://www.baeldung-cn.com/linux/bcrypt-hash
# 显示菜单
show_menu() {
    echo "欢迎使用命令行工具"
    echo "请选择一个选项 (1-12):"
    echo "1. 安装zsh"
    echo "2. 安装nvm"
    echo "3. 安装chsrc"
    echo "4. 安装k3s"
    echo "5. k3s mirror registries"
    echo "6. 安装golang"
    echo "7. git config store"
    echo "8. docker mirror"
    echo "9. lazyvim 安装"
    echo "10. 安装neovim"
    echo "11. 安装k9s"
    echo "12. github clone"
    echo  "13. 下载helm"
    echo  "14. 下载github release"
    echo  "15. 下载ko"
    echo "20. 退出"
}

k3smirror(){
FILE="/etc/rancher/k3s/registries.yaml"
FILE_TMP="./registries.yaml"
cat > ${FILE_TMP} <<- EOF
mirrors:
  jm.docker.reg:
    endpoint:
      - "http://127.0.0.1:5000"
  registry.local.w7.cc:
    endpoint:
      - "http://registry.local.w7.cc"
      - "http://127.0.0.1:32000"
  docker.io:
    endpoint:
      - "https://mirror.ccs.tencentyun.com"
      - "https://registry.cn-hangzhou.aliyuncs.com"
      - "https://docker.m.daocloud.io"
      - "https://docker.1panel.live"
  quay.io:
    endpoint:
      - "https://quay.m.daocloud.io"
      - "https://quay.dockerproxy.com"
  gcr.io:
    endpoint:
      - "https://gcr.m.daocloud.io"
      - "https://gcr.dockerproxy.com"
  ghcr.io:
    endpoint:
      - "https://ghcr.m.daocloud.io"
      - "https://ghcr.dockerproxy.com"
  k8s.gcr.io:
    endpoint:
      - "https://k8s-gcr.m.daocloud.io"
      - "https://k8s.dockerproxy.com"
  registry.k8s.io:
    endpoint:
      - "https://k8s.m.daocloud.io"
      - "https://k8s.dockerproxy.com"
  mcr.microsoft.com:
    endpoint:
      - "https://mcr.m.daocloud.io"
      - "https://mcr.dockerproxy.com"
  nvcr.io:
    endpoint:
      - "https://nvcr.m.daocloud.io"
EOF

sudo mkdir -p /etc/rancher/k3s
sudo mv $FILE_TMP $FILE
rm -rf $FILE_TMP
}

# k9splugin() {
# FILE="~/.config/k9s/plugin.yaml"
# FILE_TMP="./registries.yaml"
# cat > "~/.config/k9s/plugin.yaml" <<- EOF
# plugins:

#   # Defines a plugin to provide a `ctrl-l` shortcut to tail the logs while in pod view.
#   fred:
#     # Define a mnemonic to invoke the plugin
#     shortCut: m
#     # What will be shown on the K9s menu
#     description: debugpod
#     # Collections of views that support this shortcut. (You can use `all`)
#     scopes:
#     - all
#     # The command to run upon invocation. Can use Krew plugins here too!
#     command: kubectl
#     # Whether or not to run the command in background mode
#     background: false
#     # Defines the command arguments
#     args:
#     - debug
#     - -it
#     - pod/$POD
#     - -n
#     - $NAMESPACE
#     - --image
#     - ccr.ccs.tencentyun.com/afan-public/k8s:lazynvim-commit
#     - --target
#     - $NAME
#     - --
#     - /bin/bash
# }


# 根据用户选择执行相应的命令
execute_command() {
    case $1 in
        1)
            echo "安装zsh..."
            export REMOTE=https://gitee.com/mirrors/oh-my-zsh.git && sh -c "$(curl -fsSL https://install.ohmyz.sh/)"
            git clone https://gitee.com/umbel/zsh-autosuggestions.git ${ZSH_CUSTOM:-~/.oh-my-zsh/}/plugins/zsh-autosuggestions
            sed -i.bak 's/^plugins=(git)$/plugins=(git zsh-autosuggestions z helm kubectl docker)/' ~/.zshrc
            source ~/.zshrc
            ;;
        2)
            echo "安装nvm"
            bash -c "$(curl -fsSL https://gitee.com/RubyMetric/nvm-cn/raw/main/install.sh)"
            ;;
        3)
            echo "安装chsrc"
            curl -L https://gitee.com/RubyMetric/chsrc/releases/download/pre/chsrc-x64-linux -o chsrc; chmod +x ./chsrc && sudo mv ./chsrc /usr/local/bin/
            ;;
        4)
            echo "安装k3s"
            curl -sfL https://rancher-mirror.rancher.cn/k3s/k3s-install.sh | INSTALL_K3S_MIRROR=cn K3S_KUBECONFIG_MODE=644 sh -
            ;;
        5)
            echo "配置k3s mirror"
            k3smirror
            ;;
        6)
            echo "配置golang"
            wget -O ./go1.23.1.linux-amd64.tar.gz https://mirrors.aliyun.com/golang/go1.23.1.linux-amd64.tar.gz
            tar -xf ./go1.23.1.linux-amd64.tar.gz
            ;;
        7)
            echo "配置git"
            git config --global credential.helper store
            ;;
        8)
            echo "配置docker mirror"
	    sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://y5iruiyg.mirror.aliyuncs.com", "https://registry.cn-hangzhou.aliyuncs.com", "https://docker.m.daocloud.io"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
            ;;
    	9)
mv ~/.config/nvim{,.bak}
# optional but recommended
mv ~/.local/share/nvim{,.bak}
mv ~/.local/state/nvim{,.bak}
mv ~/.cache/nvim{,.bak}
git clone https://github.moeyy.xyz/https://github.com/LazyVim/starter ~/.config/nvim
    	;;
        10)
        wget https://github.moeyy.xyz/https://github.com/neovim/neovim/releases/latest/download/nvim-linux64.tar.gz
        sudo rm -rf /opt/nvim
        sudo tar -C /opt -xzf nvim-linux64.tar.gz
        export PATH="$PATH:/opt/nvim-linux64/bin"
        ;;
        11)
        wget https://github.moeyy.xyz/https://github.com/derailed/k9s/releases/download/v0.32.5/k9s_Linux_amd64.tar.gz
        sudo tar -C /usr/local/bin/ -xzf k9s_Linux_amd64.tar.gz
        ;;
        12)
        read -p "请输入github地址: " githuburl
        git clone https://github.moeyy.xyz/$githuburl
        ;;
        13)
        wget https://get.helm.sh/helm-v3.16.3-linux-amd64.tar.gz
        tar -xf ./helm-v3.16.3-linux-amd64.tar.gz
        mv ./linux-amd64/helm /usr/local/bin/
        ;;
        14)
        read -p "请输入release地址: " githubreleaseurl
        wget https://github.moeyy.xyz/$githubreleaseurl
        ;;
        15)
	go install github.com/google/ko@latest
        ;;
    	911)
            echo "正在修改用户名密码..."
            read -p "请输入新用户名: " new_username
            echo "正在修改密码..."
            read -sp "请输入新密码: " new_password
            echo ""
            read -sp "请再次输入新密码: " confirm_password
            echo ""
            if [ "$new_password" == "$confirm_password" ]; then
                echo "新密码: $new_password"
                kubectl get pods -l app=k8s-offline -o name | xargs -I {} kubectl exec {} -- k8s-offline auth:register --username=$new_username --password=$confirm_password
            else
                echo "密码不匹配，请重试。"
            fi
            ;;
        101)
            echo "正在更新..."
            updateImage
            ;;
        20)
            echo "退出命令行工具"
            exit 0
            ;;
        *)
            echo "无效的选择，请输入1-12之间的数字。"
            ;;
    esac
}

# 主循环
# while true; do
    show_menu
    read -p "请输入选项: " choice
    execute_command $choice
    echo ""  # 输出空行以分隔命令输出
# done
